package com.szf.mywiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szf.mywiki.domain.Content;
import com.szf.mywiki.domain.Doc;
import com.szf.mywiki.domain.DocExample;
import com.szf.mywiki.exception.BusinessException;
import com.szf.mywiki.exception.BusinessExceptionCode;
import com.szf.mywiki.mapper.ContentMapper;
import com.szf.mywiki.mapper.DocCustMapper;
import com.szf.mywiki.mapper.DocMapper;
import com.szf.mywiki.req.DocQueryReq;
import com.szf.mywiki.req.DocSaveReq;
import com.szf.mywiki.resp.DocQueryResp;
import com.szf.mywiki.resp.PageResp;
import com.szf.mywiki.util.CopyUtil;
import com.szf.mywiki.util.RedisUtil;
import com.szf.mywiki.util.RequestContext;
import com.szf.mywiki.util.SnowFlake;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DocService {

    @Resource
    private DocMapper docMapper;

    @Resource
    private DocCustMapper docCustMapper;

    @Resource
    private ContentMapper contentMapper;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private WsService wsService;

    public PageResp<DocQueryResp> list(DocQueryReq req) {

        DocExample example = new DocExample();
        example.setOrderByClause("sort asc");
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Doc> docList = docMapper.selectByExample(example);
        PageInfo<Doc> pageInfo = new PageInfo<>(docList);

        List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);

        PageResp<DocQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(respList);

        return pageResp;
    }

    public List<DocQueryResp> all(Long ebookId) {
        DocExample example = new DocExample();
        example.createCriteria().andEbookIdEqualTo(ebookId);
        example.setOrderByClause("sort asc");
        List<Doc> docList = docMapper.selectByExample(example);
        List<DocQueryResp> respList = CopyUtil.copyList(docList, DocQueryResp.class);
        return respList;
    }

    @Transactional
    public void save(DocSaveReq req) {
        Doc doc = CopyUtil.copy(req, Doc.class);
        Content content = CopyUtil.copy(req, Content.class);
        if (ObjectUtils.isEmpty(doc.getId())) {
            doc.setId(snowFlake.nextId());
            doc.setViewCount(0);
            doc.setVoteCount(0);
            docMapper.insert(doc);
            content.setId(doc.getId());
            contentMapper.insert(content);
        } else {
            docMapper.updateByPrimaryKey(doc);
            int i = contentMapper.updateByPrimaryKeyWithBLOBs(content);
            if (i == 0) {
                contentMapper.insert(content);
            }
        }
    }

    public void delete(List<String> ids) {
        DocExample example = new DocExample();
        DocExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        docMapper.deleteByExample(example);
    }

    public String findContent(Long id) {
        Content content = contentMapper.selectByPrimaryKey(id);
        docCustMapper.increaseViewCount(id);
        if (ObjectUtils.isEmpty(content)) {
            return "";
        } else {
            return content.getContent();
        }
    }

    public void vote(Long id) {
        String ip = RequestContext.getRemoteAddr();
        if (redisUtil.validateRepeat("DOC_VOTE" + id + "_" + ip, 3600 * 24)) {
            docCustMapper.increaseVoteCount(id);
        } else {
            throw new BusinessException(BusinessExceptionCode.VOTE_REPEAT);
        }

        Doc doc = docMapper.selectByPrimaryKey(id);
        String logId = MDC.get("LOG_ID");
        wsService.sendInfo("【" + doc.getName() + "】被点赞！", logId);
    }

    public void updateEbookInfo() {
        docCustMapper.updateEbookInfo();
    }

}
